﻿using System;
using System.Collections.ObjectModel;
using Common.Constants;

namespace ServicesDirectory.UserControl
{
    public partial class LookupRole : System.Web.UI.UserControl
    {
        /// <summary>
        /// Occurs when [close control].
        /// </summary>
        public event EventHandler CloseControl;

        /// <summary>
        /// Occurs when [get roles].
        /// </summary>
        public event EventHandler GetRoles;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            btnOk.Click += BtnOk_Click;
            btnCancel.Click += BtnCancel_Click;
        }

        /// <summary>
        /// Handles the Click event of the BtnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void BtnOk_Click(object sender, EventArgs e)
        {
            var arrayroles = new Collection<int>();
            if(cbFunder.Checked)
            {
                arrayroles.Add((int)OrgRole.Funder);
            }
            if (cbLead.Checked)
            {
                arrayroles.Add((int)OrgRole.Lead);
            }
            if (cbDelivery.Checked)
            {
                arrayroles.Add((int)OrgRole.Delivery);
            }
            if (cbAuditor.Checked)
            {
                arrayroles.Add((int)OrgRole.Auditor);
            }
            if (GetRoles!= null)
            {
                GetRoles(arrayroles, e);
            }
            if (CloseControl != null)
            {
                CloseControl(sender, e);
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            if (CloseControl!=null)
            {
                CloseControl(sender, e);
            }
        }

        /// <summary>
        /// Binds the data.
        /// </summary>
        /// <param name="roles">The roles.</param>
        public void BindData(string roles)
        {
            cbAuditor.Checked = false;
            cbFunder.Checked = false;
            cbDelivery.Checked = false;
            cbLead.Checked = false;
            var arrayroles = roles.Split('#');
            foreach (var role in arrayroles)
            {
                switch (role)
                {
                    case "1":
                        cbFunder.Checked = true;
                        break;
                    case "2":
                        cbLead.Checked = true;
                        break;
                    case "3":
                        cbDelivery.Checked = true;
                        break;
                    case "4":
                        cbAuditor.Checked = true;
                        break;
                }
            }
        }
    }
}